<?php
/**
 * @description : OAuth Mobile Ajax Support Page.
 * @author      : stephen.mo
 * @date        : 2010/09/10
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set("display_errors","on");

$access_token = (isset($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL;
$login_id     = (isset($_REQUEST['login_id'])) ? $_REQUEST['login_id'] : NULL;
$type         = (isset($_REQUEST['item'])) ? $_REQUEST['item'] : NULL;
$lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$tmp_type     = (isset($_REQUEST['type'])) ? $_REQUEST['type'] : NULL;

//special vars.
//need to check page language.
$need_chk_plang = OAuthModelConfig::$need_chk_plang;
if (in_array($type,$need_chk_plang)) {
    $lang = (isset($_REQUEST['p_lang'])) ? OAuthUtils::chkLang($_REQUEST['p_lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
}

//need to check oauth token. need to open check oauth-token.
if (OAuthModelConfig::IS_NEED_CHECK_OAUTHTOKEN) {/*{{{*/ 
    $typeAllows = OAuthModelConfig::$allow_ajax_actions;
    if (!in_array($type,$typeAllows)) {
        if (empty($access_token)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1005,$lang,'','oauth_token');
            exit;
        }
        $ret = OAuthModel::getInstance()->verifyAccessToken($access_token);
        if (!$ret || !$ret['flag']) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1005,$lang,'','oauth_token');
            exit;
        }
    }
}/*}}}*/

//do logic by type.
switch($type) {
    case 'orderRefFree_special':/*{{{*/
        //get captcha_token
        $lang         = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : 'zh-hant-hk';
        $captcha_code = (isset($_REQUEST['captcha'])) ? $_REQUEST['captcha'] : NULL;
        $pukey        = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
        $captcha_info = OAuthModel::getInstance()->getCaptchaToken();
        if (!isset($captcha_info['captcha_tokens']) || empty($captcha_info['captcha_tokens']) || empty($captcha_code)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1020,$lang,'','captcha');
            exit;
        }
        $captcha_info['captcha'] = $captcha_code;
        $reg = OAuthModel::getInstance()->chkCaptchaToken($captcha_info);
        if (!$reg) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','captcha');
            exit;
        }
        
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','PUKey');
            exit;
        }

        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if (!$tmpData->retFlag) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','getFormData');
            exit;
        }
        $fdata = array();
        $tmpData = $tmpData->data;
        $fdata['m_call_back']    = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']         = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']   = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']        = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['p_plan_id']      = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
        $fdata['p_version_id']   = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
        $fdata['p_bill_address'] = (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : '';
        $fdata['p_pwd']          = (isset($tmpData->p_pwd)) ? $tmpData->p_pwd : NULL;
        $fdata['p_repwd']        = (isset($tmpData->p_repwd)) ? $tmpData->p_repwd : NULL;
        $fdata['p_lang']         = (isset($tmpData->p_lang)) ? $tmpData->p_lang : '';
        $fdata['current_page']   = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_PAYMENT; 
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['m_step_back_IV']  = (isset($tmpData->m_step_back_IV)) ? $tmpData->m_step_back_IV : '';
        $fdata['m_step_back_V']   = (isset($tmpData->m_step_back_V)) ? $tmpData->m_step_back_V : '';
        $fdata['p_reg_flag']      = (isset($tmpData->p_reg_flag)) ? $tmpData->p_reg_flag : 1;
        $fdata['p_from']          = (isset($tmpData->p_from)) ? $tmpData->p_from : OAuthModelConfig::OAUTH_MOBILE_FLAG;

        //get parameters
        $package_id = (isset($_REQUEST['pkgInfo_id'])) ? $_REQUEST['pkgInfo_id'] : (isset($tmpData->p_plan_id) ? $tmpData->p_plan_id : NULL);
        $version_id = (isset($_REQUEST['pkgInfo_version'])) ? $_REQUEST['pkgInfo_version'] : (isset($tmpData->p_version_id) ? $tmpData->p_version_id : NULL);
        if (empty($package_id) || empty($version_id)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','pkgInfo_id');
            exit;
        }

        $email_address= (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : NULL;
        
        //get user info by login_id.
        $user_country = NULL;
        $userInfo     = OAuthModel::getInstance()->getUserInfoByLoginId($login_id);
        if ($userInfo->retFlag==1) {
            $user_country = $userInfo->result->country;
        }
        if (empty($email_address)) {
            $user_email = $userInfo->result->email;
        }
        else {
            $user_email = $email_address;
        }

        //set form data.
        $params = array();
        $fdata['p_username']        = NULL;
        $fdata['p_plan_id']         = $package_id;
        $fdata['p_version_id']      = $version_id;
        $fdata['is_submit']         = 0;
        $fdata['if_change_plan']    = 0;
        $fdata['p_country']         = $user_country;
        $fdata['p_bill_address']    = $user_email;

        //set data to memcache
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','setFormData');
            exit;
        }
        
        //assign vars
        $agree_term = (isset($_REQUEST['agree_term'])) ? $_REQUEST['agree_term'] : NULL;

        //genNewOrderPlan()
        $params['agree_term']    = '1';
        $params['captcha']       = $captcha_info['captcha'];
        $params['captcha_token'] = $captcha_info['captcha_tokens'];
        $params['ptoken']        = $ptoken;
        $params['pukey']         = $pukey;
        
        $ret = OAuthModel::getInstance()->getOrderIdOfPayment($params,'free');
        if ($ret->retFlag != 1) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1028,$lang,'','get_order_ref');
            exit;
        }
        $order_ref_ = json_decode($ret->data);
        echo OAuthModelConfig::getAjaxErrInfo(1,1,$lang,'ok','order_ref',$order_ref_->order_ref);
    break;/*}}}*/
    case 'orderRef_special':/*{{{*/
        //get captcha_token
        $lang         = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : 'zh-hant-hk';
        $captcha_code = (isset($_REQUEST['captcha'])) ? $_REQUEST['captcha'] : NULL;
        $pukey        = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
        $captcha_info = OAuthModel::getInstance()->getCaptchaToken();
        if (!isset($captcha_info['captcha_tokens']) || empty($captcha_info['captcha_tokens']) || empty($captcha_code)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1020,$lang,'','captcha');
            exit;
        }
        $captcha_info['captcha'] = $captcha_code;
        $reg = OAuthModel::getInstance()->chkCaptchaToken($captcha_info);
        if (!$reg) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','captcha');
            exit;
        }
        
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','PUKey');
            exit;
        }

        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if (!$tmpData->retFlag) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','getFormData');
            exit;
        }
        $fdata = array();
        $tmpData = $tmpData->data;
        $fdata['m_call_back']    = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']         = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']   = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']        = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['p_plan_id']      = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
        $fdata['p_version_id']   = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
        $fdata['p_bill_address'] = (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : '';
        $fdata['p_pwd']          = (isset($tmpData->p_pwd)) ? $tmpData->p_pwd : NULL;
        $fdata['p_repwd']        = (isset($tmpData->p_repwd)) ? $tmpData->p_repwd : NULL;
        $fdata['p_lang']         = (isset($tmpData->p_lang)) ? $tmpData->p_lang : '';
        $fdata['current_page']   = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_PAYMENT; 
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['m_step_back_IV']  = (isset($tmpData->m_step_back_IV)) ? $tmpData->m_step_back_IV : '';
        $fdata['m_step_back_V']   = (isset($tmpData->m_step_back_V)) ? $tmpData->m_step_back_V : '';
        $fdata['p_reg_flag']      = (isset($tmpData->p_reg_flag)) ? $tmpData->p_reg_flag : 1;
        $fdata['p_from']          = (isset($tmpData->p_from)) ? $tmpData->p_from : OAuthModelConfig::OAUTH_MOBILE_FLAG;

        //get parameters
        $package_id = (isset($_REQUEST['pkgInfo_id'])) ? $_REQUEST['pkgInfo_id'] : (isset($tmpData->p_plan_id) ? $tmpData->p_plan_id : NULL);
        $version_id = (isset($_REQUEST['pkgInfo_version'])) ? $_REQUEST['pkgInfo_version'] : (isset($tmpData->p_version_id) ? $tmpData->p_version_id : NULL);
        if (empty($package_id) || empty($version_id)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','pkgInfo_id');
            exit;
        }

        $email_address= (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : NULL;
        
        //get user info by login_id.
        $user_country = NULL;
        $userInfo     = OAuthModel::getInstance()->getUserInfoByLoginId($login_id);
        if ($userInfo->retFlag==1) {
            $user_country = $userInfo->result->country;
        }
        if (empty($email_address)) {
            $user_email = $userInfo->result->email;
        }
        else {
            $user_email = $email_address;
        }

        //set form data.
        $params = array();
        $fdata['p_username']        = NULL;
        $fdata['p_plan_id']         = $package_id;
        $fdata['p_version_id']      = $version_id;
        $fdata['is_submit']         = 0;
        $fdata['if_change_plan']    = 0;
        $fdata['p_country']         = $user_country;
        $fdata['p_bill_address']    = $user_email;

        //set data to memcache
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','setFormData');
            exit;
        }
        
        //assign vars
        $card_type  = (isset($_REQUEST['card_type'])) ? $_REQUEST['card_type'] : NULL;
        $card_owner = (isset($_REQUEST['card_owner'])) ? $_REQUEST['card_owner'] : NULL;
        $card_no    = (isset($_REQUEST['card_no'])) ? $_REQUEST['card_no'] : NULL;
        $expiry_y   = (isset($_REQUEST['expiry_y'])) ? $_REQUEST['expiry_y'] : NULL;
        $expiry_m   = (isset($_REQUEST['expiry_m'])) ? $_REQUEST['expiry_m'] : NULL;
        $card_cvv   = (isset($_REQUEST['card_cvv'])) ? $_REQUEST['card_cvv'] : NULL;
        $agree_term = (isset($_REQUEST['agree_term'])) ? $_REQUEST['agree_term'] : NULL;
        if (!OAuthModel::getInstance()->isValidCreditCard($card_no)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1016,$lang,'','card_no');
            exit;
        }
        if (empty($card_owner)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1014,$lang,'','card_owner');
            exit;
        }
        if (empty($expiry_y) ) { 
            echo OAuthModelConfig::getAjaxErrInfo(0,1018,$lang,'','expiry_y');
            exit;
        }
        if (empty($expiry_m)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1018,$lang,'','expiry_m');
            exit;
        }
        if (!OAuthModel::getInstance()->isValidExpiryDate($expiry_y,$expiry_m)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1018,$lang,'','expiry_y');
            exit;
        }
        if (empty($card_cvv)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1017,$lang,'','card_cvv');
            exit;
        }

        //genNewOrderPlan()
        $params['card_type']  = $card_type;
        $params['card_owner'] = $card_owner;
        $params['card_no']    = $card_no;
        $params['expiry_m']   = $expiry_m;
        $params['expiry_y']   = $expiry_y;
        $params['card_cvv']   = $card_cvv;
        $params['agree_term'] = '1';
        $params['captcha']    = $captcha_info['captcha'];
        $params['captcha_token'] = $captcha_info['captcha_tokens'];
        $params['ptoken']     = $ptoken;
        $params['pukey']      = $pukey;
        
        $ret = OAuthModel::getInstance()->getOrderIdOfPayment($params);
        if ($ret->retFlag != 1) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1028,$lang,'','get_order_ref');
            exit;
        }
        $order_ref_ = json_decode($ret->data);
        echo OAuthModelConfig::getAjaxErrInfo(1,1,$lang,'ok','order_ref',$order_ref_->order_ref);
        break;/*}}}*/
    case 'orderRefFree': /*{{{*/
        //get captcha_token
        $lang         = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : 'zh-hant-hk';
        $captcha_code = (isset($_REQUEST['captcha'])) ? $_REQUEST['captcha'] : NULL;
        $pukey        = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
        $captcha_info = OAuthModel::getInstance()->getCaptchaToken();
        if (!isset($captcha_info['captcha_tokens']) || empty($captcha_info['captcha_tokens']) || empty($captcha_code)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1020,$lang,'','captcha');
            exit;
        }
        $captcha_info['captcha'] = $captcha_code;
        $reg = OAuthModel::getInstance()->chkCaptchaToken($captcha_info);
        if (!$reg) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','captcha');
            exit;
        }
        
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','PUKey');
            exit;
        }

        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if (!$tmpData->retFlag) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','getFormData');
            exit;
        }
        $fdata = array();
        $tmpData = $tmpData->data;
        $fdata['m_login_id']     = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
        $fdata['m_call_back']    = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']         = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']   = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']        = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['p_plan_id']      = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
        $fdata['p_version_id']   = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
        $fdata['p_bill_address'] = (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : '';
        $fdata['p_lang']         = (isset($tmpData->p_lang)) ? $tmpData->p_lang : '';
        $fdata['current_page']   = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_PAYMENT; 
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['m_step_back_IV']  = (isset($tmpData->m_step_back_IV)) ? $tmpData->m_step_back_IV : '';
        $fdata['m_step_back_V']   = (isset($tmpData->m_step_back_V)) ? $tmpData->m_step_back_V : '';

        //get parameters
        $package_id = (isset($_REQUEST['pkgInfo_id'])) ? $_REQUEST['pkgInfo_id'] : (isset($tmpData->p_plan_id) ? $tmpData->p_plan_id : NULL);
        $version_id = (isset($_REQUEST['pkgInfo_version'])) ? $_REQUEST['pkgInfo_version'] : (isset($tmpData->p_version_id) ? $tmpData->p_version_id : NULL);
        if (empty($package_id) || empty($version_id)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','pkgInfo_id');
            exit;
        }

        $email_address= (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : NULL;
        
        //get user info by login_id.
        $user_country = NULL;
        $userInfo     = OAuthModel::getInstance()->getUserInfoByLoginId($login_id);
        if ($userInfo->retFlag==1) {
            $user_country = $userInfo->result->country;
        }
        if (empty($email_address)) {
            $user_email = $userInfo->result->email;
        }
        else {
            $user_email = $email_address;
        }

        //set form data.
        $params = array();
        $fdata['p_username']        = $login_id;
        $fdata['p_plan_id']         = $package_id;
        $fdata['p_version_id']      = $version_id;
        $fdata['is_submit']         = 0;
        $fdata['if_change_plan']    = 0;
        $fdata['p_country']         = $user_country;
        $fdata['p_bill_address']    = $user_email;
        $fdata['oauth_token']       = $access_token;
        $fdata['p_reg_flag']        =  0;

        //set data to memcache
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','setFormData');
            exit;
        }
        
        //assign vars
        $agree_term = (isset($_REQUEST['agree_term'])) ? $_REQUEST['agree_term'] : NULL;

        //genNewOrderPlan()
        $params['agree_term']    = '1';
        $params['captcha']       = $captcha_info['captcha'];
        $params['captcha_token'] = $captcha_info['captcha_tokens'];
        $params['ptoken']        = $ptoken;
        $params['pukey']         = $pukey;
        
        $ret = OAuthModel::getInstance()->getOrderIdOfPayment($params,'free');
        if ($ret->retFlag != 1) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1028,$lang,'','get_order_ref');
            exit;
        }
        $order_ref_ = json_decode($ret->data);
        echo OAuthModelConfig::getAjaxErrInfo(1,1,$lang,'ok','order_ref',$order_ref_->order_ref);
        break;/*}}}*/
    case 'orderRef':/*{{{*/
        //get captcha_token
        $lang         = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : 'zh-hant-hk';
        $captcha_code = (isset($_REQUEST['captcha'])) ? $_REQUEST['captcha'] : NULL;
        $pukey        = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
        $captcha_info = OAuthModel::getInstance()->getCaptchaToken();
        if (!isset($captcha_info['captcha_tokens']) || empty($captcha_info['captcha_tokens']) || empty($captcha_code)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1020,$lang,'','captcha');
            exit;
        }
        $captcha_info['captcha'] = $captcha_code;
        $reg = OAuthModel::getInstance()->chkCaptchaToken($captcha_info);
        if (!$reg) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','captcha');
            exit;
        }
        
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','PUKey');
            exit;
        }

        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if (!$tmpData->retFlag) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','getFormData');
            exit;
        }
        $fdata = array();
        $tmpData = $tmpData->data;
        $fdata['m_login_id']     = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
        $fdata['m_call_back']    = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']         = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']   = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']        = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['p_plan_id']      = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
        $fdata['p_version_id']   = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
        $fdata['p_bill_address'] = (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : '';
        $fdata['p_lang']         = (isset($tmpData->p_lang)) ? $tmpData->p_lang : '';
        $fdata['current_page']   = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_PAYMENT; 
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['m_step_back_IV']  = (isset($tmpData->m_step_back_IV)) ? $tmpData->m_step_back_IV : '';
        $fdata['m_step_back_V']   = (isset($tmpData->m_step_back_V)) ? $tmpData->m_step_back_V : '';

        //get parameters
        $package_id = (isset($_REQUEST['pkgInfo_id'])) ? $_REQUEST['pkgInfo_id'] : (isset($tmpData->p_plan_id) ? $tmpData->p_plan_id : NULL);
        $version_id = (isset($_REQUEST['pkgInfo_version'])) ? $_REQUEST['pkgInfo_version'] : (isset($tmpData->p_version_id) ? $tmpData->p_version_id : NULL);
        if (empty($package_id) || empty($version_id)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','pkgInfo_id');
            exit;
        }

        $email_address= (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : NULL;
        
        //get user info by login_id.
        $user_country = NULL;
        $userInfo     = OAuthModel::getInstance()->getUserInfoByLoginId($login_id);
        if ($userInfo->retFlag==1) {
            $user_country = $userInfo->result->country;
        }
        if (empty($email_address)) {
            $user_email = $userInfo->result->email;
        }
        else {
            $user_email = $email_address;
        }

        //set form data.
        $params = array();
        $fdata['p_username']        = $login_id;
        $fdata['p_plan_id']         = $package_id;
        $fdata['p_version_id']      = $version_id;
        $fdata['is_submit']         = 0;
        $fdata['if_change_plan']    = 0;
        $fdata['p_country']         = $user_country;
        $fdata['p_bill_address']    = $user_email;
        $fdata['oauth_token']       = $access_token;
        $fdata['p_reg_flag']        = 0;

        //set data to memcache
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','setFormData');
            exit;
        }

        //assign vars
        $card_type  = (isset($_REQUEST['card_type'])) ? $_REQUEST['card_type'] : NULL;
        $card_owner = (isset($_REQUEST['card_owner'])) ? $_REQUEST['card_owner'] : NULL;
        $card_no    = (isset($_REQUEST['card_no'])) ? $_REQUEST['card_no'] : NULL;
        $expiry_y   = (isset($_REQUEST['expiry_y'])) ? $_REQUEST['expiry_y'] : NULL;
        $expiry_m   = (isset($_REQUEST['expiry_m'])) ? $_REQUEST['expiry_m'] : NULL;
        $card_cvv   = (isset($_REQUEST['card_cvv'])) ? $_REQUEST['card_cvv'] : NULL;
        $agree_term = (isset($_REQUEST['agree_term'])) ? $_REQUEST['agree_term'] : NULL;
        if (!OAuthModel::getInstance()->isValidCreditCard($card_no)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1016,$lang,'','card_no');
            exit;
        }
        if (empty($card_owner)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1014,$lang,'','card_owner');
            exit;
        }
        if (empty($expiry_y) ) { 
            echo OAuthModelConfig::getAjaxErrInfo(0,1018,$lang,'','expiry_y');
            exit;
        }
        if (empty($expiry_m)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1018,$lang,'','expiry_m');
            exit;
        }
        if (!OAuthModel::getInstance()->isValidExpiryDate($expiry_y,$expiry_m)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1018,$lang,'','expiry_y');
            exit;
        }
        if (empty($card_cvv)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1017,$lang,'','card_cvv');
            exit;
        }

        //genNewOrderPlan()
        $params['card_type']  = $card_type;
        $params['card_owner'] = $card_owner;
        $params['card_no']    = $card_no;
        $params['expiry_m']   = $expiry_m;
        $params['expiry_y']   = $expiry_y;
        $params['card_cvv']   = $card_cvv;
        $params['agree_term'] = '1';
        $params['captcha']    = $captcha_info['captcha'];
        $params['captcha_token'] = $captcha_info['captcha_tokens'];
        $params['ptoken']     = $ptoken;
        $params['pukey']      = $pukey;
        
        $ret = OAuthModel::getInstance()->getOrderIdOfPayment($params);
        if ($ret->retFlag != 1) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1028,$lang,'','get_order_ref');
            exit;
        }
        $order_ref_ = json_decode($ret->data);
        //echo OAuthModelConfig::getAjaxErrInfo(1,1,$lang,'ok','order_ref',$order_ref_->order_ref,'remark',$order_ref_->remark);
        echo OAuthModelConfig::getAjaxReturnInfo(1,1,$lang,'ok',array('order_ref'=>$order_ref_->order_ref,'remark'=>$order_ref_->remark));
        break;/*}}}*/
    case 'userInfo':/*{{{*/
        //save user info to mc.
        $user_account_email = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $user_page_lang     = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : NULL;
        $pkg_id             = (isset($_REQUEST['pkg_id'])) ? $_REQUEST['pkg_id'] : NULL;
        $pkg_version        = (isset($_REQUEST['pkg_v'])) ? $_REQUEST['pkg_v'] : NULL;
        $pukey              = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;

        //check vars
        if (empty($user_account_email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','a_email');
            exit;
        }
        if (empty($pkg_id) || empty($pkg_version)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','package_info');
            exit; 
        }
        
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','PUKey');
            exit;
        }
        
        //bind datas.
        $fdata = array();
        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if (!$tmpData->retFlag) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','getFormData');
            exit;
        }

        $tmpData = $tmpData->data;
        $fdata['m_login_id']     = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
        $fdata['m_call_back']    = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']         = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']   = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']        = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['p_plan_id']      = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
        $fdata['p_version_id']   = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
        $fdata['current_page']   = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_INFORMATION;
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['m_step_back_IV']  = (isset($tmpData->m_step_back_IV)) ? $tmpData->m_step_back_IV : '';
        $fdata['m_step_back_V']   = (isset($tmpData->m_step_back_V)) ? $tmpData->m_step_back_V : '';
        
        $fdata['p_bill_address'] = $user_account_email;
        $fdata['p_lang']         = $user_page_lang;
 
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','setFormData');
            die();
        }
        $result['retFlag'] = 1;
        echo json_encode($result);
        break;/*}}}*/
    case 'joinUserInfo':/*{{{*/
        $email      = (isset($_REQUEST['a_email'])) ? $_REQUEST['a_email'] : NULL;
        $password   = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : NULL;
        $repassword = (isset($_REQUEST['repassword'])) ? $_REQUEST['repassword'] : NULL;
        $p_lang     = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : NULL;
        $lang       = (isset($_REQUEST['lang'])) ? $_REQUEST['lang'] : NULL;
        $pkg_id     = (isset($_REQUEST['pkg_id'])) ? $_REQUEST['pkg_id'] : NULL;
        $pkg_version= (isset($_REQUEST['pkg_v'])) ? $_REQUEST['pkg_v'] : NULL;
        $pukey      = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;

        //check the field.
        $reg_mail = "/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/";
        if (empty($email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','email');
            exit;
        }
        if (!preg_match($reg_mail,$email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1002,$lang,'','email');
            exit;
        }
        if (empty($password)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1003,$lang,'','password');
            exit;
        }
        if (empty($repassword)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1008,$lang,'','repassword');
            exit;
        }
        if ($password!=$repassword) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1007,$lang,'','password');
            exit;
        }
        if (empty($pkg_id) || empty($pkg_version)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','package');
            exit;
        }
        
        //check email is exists.
        $ret = OAuthModel::getInstance()->chkEmailIsExist($email);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1011,$lang,'','email');
            exit; 
        }
        
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','PUKey');
            exit;
        }

        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if ($tmpData->retFlag != 1) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','getFormData');
            exit;
        }
        $tmpData = $tmpData->data;
        $fdata = array();
        $fdata['m_call_back']     = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']          = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']    = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']         = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_stepm_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['current_page']     = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_JJOIN;
        $fdata['p_plan_id']        = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : '';
        $fdata['p_version_id']     = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : '';
        
        //set form datas.
        $fdata['p_reg_flag']     = 1;
        $fdata['p_pwd']          = $password;
        $fdata['p_repwd']        = $password;
        $fdata['p_lang']         = $p_lang;
        $fdata['p_bill_address'] = $email;
        $fdata['p_from']         = OAuthModelConfig::OAUTH_MOBILE_FLAG;
        
        //save form data to memcache.
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','setFormData');
            exit;
        }
        $result['retFlag'] = 1;
        $result['data']    = NULL;
        echo json_encode($result);
        break;/*}}}*/
    case 'memberReg':/*{{{*/
        $email        = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $password     = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : NULL;
        $repassword   = (isset($_REQUEST['repassword'])) ? $_REQUEST['repassword'] : NULL;
        $p_lang       = (isset($_REQUEST['p_lang'])) ? OAuthUtils::chkLang($_REQUEST['p_lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $captcha_code = (isset($_REQUEST['captcha'])) ? $_REQUEST['captcha'] : NULL;

        //check captcha code.
        $captcha_info = OAuthModel::getInstance()->getCaptchaToken();
        if (!isset($captcha_info['captcha_tokens']) || empty($captcha_info['captcha_tokens']) || empty($captcha_code)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1020,$lang,'','captcha');
            exit;
        }
        $captcha_info['captcha'] = $captcha_code;
        $reg = OAuthModel::getInstance()->chkCaptchaToken($captcha_info);
        if (!$reg) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','captcha');
            exit;
        }
        
        //check the field.
        if (empty($email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','email');
            exit;
        }
        if (!preg_match(OAuthModelConfig::DEFAULT_EMAIL_PREG_MATCH,$email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1002,$lang,'','email');
            exit;
        }
        if (empty($password)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1003,$lang,'','password');
            exit;
        }
        if (empty($repassword)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1008,$lang,'','repassword');
            exit;
        }
        if ($password!=$repassword) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1007,$lang,'','password');
            exit; 
        }
        
        //check this email is exists. 
        $ret = OAuthModel::getInstance()->chkEmailIsExist($email);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1011,$lang,'','email');
            exit; 
        }
        //call register API.
        $ret = OAuthModel::getInstance()->quicklyRegisterMember($email,$password,$lang,$p_lang);
        if ($ret['retFlag']==0) {
            echo OAuthModelConfig::getAjaxErrInfo(0,$ret['errCode'],$lang,'','register');
            exit;
        }
        echo OAuthModelConfig::getAjaxReturnInfo(1,1,$lang,'ok',NULL);
        break;/*}}}*/
    case 'forgetPassword':/*{{{*/
        //call forget password API.
        $email = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        if (empty($email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','email');
            exit;
        }       
        if (!preg_match(OAuthModelConfig::DEFAULT_EMAIL_PREG_MATCH,$email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1002,$lang,'','email');
            exit;
        }
        //find your password with your email address.
        $ret = OAuthUtils::forgetPassword($email);
        if ($ret) {
            echo OAuthModelConfig::getAjaxReturnInfo(1,1038,$lang,'ok',array('rep'=>$email,'tag'=>'{%EMAIL_ADDR%}','sou'=>NULL),true);
            exit;
        }
        echo OAuthModelConfig::getAjaxReturnInfo(0,1039,$lang,'error');
        break;/*}}}*/
    case 'quickSignIn':/*{{{*/
        //chk email address and password.
        $email    = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $password = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : NULL;
        $lang     = (isset($_REQUEST['lang'])) ? $_REQUEST['lang'] : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $pg       = (isset($_REQUEST['pg'])) ? $_REQUEST['pg'] : NULL;

        //chk inputs.
        if (empty($email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','email');
            exit;
        }
        if (!preg_match(OAuthModelConfig::DEFAULT_EMAIL_PREG_MATCH,$email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1002,$lang,'','email');
            exit;
        }
        
        //chk and try to logining.
        $rev = OAuthUtils::loginForMobile($email,$password);
        if (!$rev) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1004,$lang,'','email');
            exit;
        }
        
        $login_token = trim($rev->token);
        $member_id = $rev->member_id;
        $params = array();
        $params['user']          = $email;
        $params['pass']          = $password;
        $params['login_id']      = OAuthUtils::getLoginIdByEmail($email);
        $params['token']         = $login_token;
        $params['request_token'] = NULL;
        $params['member_id']     = $member_id;

        $tvbauth = OAuthUtils::encrypt($member_id);
        if (OAuthModelConfig::USING_MEM_SAVE_MEMBER) {
            OAuthUtils::setUserInfo($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        }
        //save user's info to memcache & save cookie info.
        $rec = OAuthUtils::saveUserInfoToMc($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        echo OAuthModelConfig::getAjaxReturnInfo(1,1,$lang,'ok',array('login_id'=>$tvbauth));
        exit;
        break;/*}}}*/
    case 'quickRegister':/*{{{*/
        $email        = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $password     = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : NULL;
        $repassword   = (isset($_REQUEST['repassword'])) ? $_REQUEST['repassword'] : NULL;
        $p_lang       = (isset($_REQUEST['p_lang'])) ? OAuthUtils::chkLang($_REQUEST['p_lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $captcha_code = (isset($_REQUEST['captcha'])) ? $_REQUEST['captcha'] : NULL;
        $pg           = (isset($_REQUEST['pg'])) ? $_REQUEST['pg'] : NULL;

        //check captcha code.
        $captcha_info = OAuthModel::getInstance()->getCaptchaToken();
        if (!isset($captcha_info['captcha_tokens']) || empty($captcha_info['captcha_tokens']) || empty($captcha_code)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1020,$lang,'','captcha');
            exit;
        }
        $captcha_info['captcha'] = $captcha_code;
        $reg = OAuthModel::getInstance()->chkCaptchaToken($captcha_info);
        if (!$reg) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','captcha');
            exit;
        }
        
        //check the field.
        /*{{{*/
        if (empty($email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','email');
            exit;
        }
        if (!preg_match(OAuthModelConfig::DEFAULT_EMAIL_PREG_MATCH,$email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1002,$lang,'','email');
            exit;
        }
        if (empty($password)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1003,$lang,'','password');
            exit;
        }
        if (empty($repassword)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1008,$lang,'','repassword');
            exit;
        }
        if ($password!=$repassword) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1007,$lang,'','password');
            exit; 
        }
        /*}}}*/

        //check this email is exists. 
        $ret = OAuthModel::getInstance()->chkEmailIsExist($email);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1011,$lang,'','email');
            exit; 
        }
        //call register API.
        $ret = OAuthModel::getInstance()->quicklyRegisterMember($email,$password,$lang,$p_lang);
        if ($ret['retFlag']==0) {
            echo OAuthModelConfig::getAjaxErrInfo(0,$ret['errCode'],$lang,'','register');
            exit;
        }
        
        //if register success then auto login in.
        $rev = OAuthUtils::loginForMobile($email,$password);
        $login_token = trim($rev->token);
        $member_id = $rev->member_id;
        $params = array();
        $params['user']          = $email;
        $params['pass']          = $password;
        $params['login_id']      = OAuthUtils::getLoginIdByEmail($email);
        $params['token']         = $login_token;
        $params['request_token'] = NULL;
        $params['member_id'] = $member_id;
        $tvbauth = OAuthUtils::encrypt($member_id);
        if (OAuthModelConfig::USING_MEM_SAVE_MEMBER) {
            OAuthUtils::setUserInfo($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        }
        //save user's info to memcache & save cookie info.
        $rec = OAuthUtils::saveUserInfoToMc($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        echo OAuthModelConfig::getAjaxReturnInfo(1,1,$lang,'ok',array('login_id'=>$tvbauth));
        break;/*}}}*/
    case 'tmpSignIn':/*{{{*/
        #step I ,login-in at first
        //chk email address and password.
        $email     = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $password  = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : NULL;
        $lang      = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $msisdn_id = (isset($_REQUEST['msisdn_id'])) ? $_REQUEST['msisdn_id'] : NULL;
        
        //chk inputs.
        if (empty($email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','email');
            exit;
        }
        if (!preg_match(OAuthModelConfig::DEFAULT_EMAIL_PREG_MATCH,$email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1002,$lang,'','email');
            exit;
        }
        //chk and try to logining.
        $rev = OAuthUtils::loginForMobile($email,$password);
        if (!$rev) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1004,$lang,'','email');
            exit;
        }
        /**
        if ($rev == -1) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1042,$lang,'','email');
            exit;
        }
        */

        $login_token = trim($rev->token);
        $member_id = $rev->member_id;
        $params = array();
        $params['user']          = $email;
        $params['pass']          = $password;
        $params['login_id']      = OAuthUtils::getLoginIdByEmail($email);
        $params['token']         = $login_token;
        $params['request_token'] = NULL;
        $params['msisdn_id']     = $msisdn_id;
        $params['member_id'] = $member_id;
        if (OAuthModelConfig::USING_MEM_SAVE_MEMBER) {
            OAuthUtils::setUserInfo($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        }

        //save user's info to memcache & save cookie info.
        $rec = OAuthUtils::saveUserInfoToMc($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        $rec = OAuthUtils::saveLoginInfo($email,$lang);
        //echo OAuthModelConfig::getAjaxReturnInfo(1,1,$lang,'ok',array('login_id'=>base64_encode($params['login_id'])));
        
        #step II ,save user info to mc.
        $user_account_email = $email;   //(isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $user_page_lang     = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : ((!empty($lang)) ? $lang : OAuthConfig::DEFAULT_SUPPORT_LANG);
        $pkg_id             = (isset($_REQUEST['pkg_id'])) ? $_REQUEST['pkg_id'] : NULL;
        $pkg_version        = (isset($_REQUEST['pkg_v'])) ? $_REQUEST['pkg_v'] : NULL;
        $pukey              = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
        
        //check vars
        if (empty($pkg_id) || empty($pkg_version)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','package_info');
            exit; 
        }
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','PUKey');
            exit;
        }
        
        //bind datas.
        $fdata = array();
        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if (!$tmpData->retFlag) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','getFormData');
            exit;
        }

        $tmpData = $tmpData->data;
        $fdata['m_login_id']      = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
        $fdata['m_call_back']     = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']          = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']    = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']         = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['p_plan_id']       = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
        $fdata['p_version_id']    = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
        $fdata['current_page']    = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_INFORMATION;
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['m_step_back_IV']  = (isset($tmpData->m_step_back_IV)) ? $tmpData->m_step_back_IV : '';
        $fdata['m_step_back_V']   = (isset($tmpData->m_step_back_V)) ? $tmpData->m_step_back_V : '';
        $fdata['p_bill_address']  = $user_account_email;
        $fdata['p_lang']          = $user_page_lang;
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','setFormData');
            exit;
        }
        $result['retFlag'] = 1;
        echo json_encode($result);
        break;/*}}}*/
    case 'tmpRegister':/*{{{*/
        #step I ,register at first.
        $email        = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $password     = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : NULL;
        $repassword   = (isset($_REQUEST['repassword'])) ? $_REQUEST['repassword'] : NULL;
        $p_lang       = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
        $captcha_code = (isset($_REQUEST['captcha'])) ? $_REQUEST['captcha'] : NULL;
        $msisdn_id    = (isset($_REQUEST['msisdn_id'])) ? $_REQUEST['msisdn_id'] : NULL;

        //check captcha code.
        $captcha_info = OAuthModel::getInstance()->getCaptchaToken();
        if (!isset($captcha_info['captcha_tokens']) || empty($captcha_info['captcha_tokens']) || empty($captcha_code)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1020,$lang,'','captcha');
            exit;
        }
        $captcha_info['captcha'] = $captcha_code;
        $reg = OAuthModel::getInstance()->chkCaptchaToken($captcha_info);
        if (!$reg) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1021,$lang,'','captcha');
            exit;
        }
        
        //check the field.
        /*{{{*/
        if (empty($email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1001,$lang,'','email');
            exit;
        }
        if (!preg_match(OAuthModelConfig::DEFAULT_EMAIL_PREG_MATCH,$email)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1002,$lang,'','email');
            exit;
        }
        if (empty($password)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1003,$lang,'','password');
            exit;
        }
        if (empty($repassword)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1008,$lang,'','repassword');
            exit;
        }
        if ($password!=$repassword) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1007,$lang,'','password');
            exit; 
        }
        /*}}}*/

        //check this email is exists. 
        $ret = OAuthModel::getInstance()->chkEmailIsExist($email);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1011,$lang,'','email');
            exit; 
        }
        //call register API.
        $ret = OAuthModel::getInstance()->quicklyRegisterMember($email,$password,$lang,$p_lang);
        if ($ret['retFlag']==0) {
            echo OAuthModelConfig::getAjaxErrInfo(0,$ret['errCode'],$lang,'','register');
            exit;
        }
        
        //if register success then auto login in.
        $rev = OAuthUtils::loginForMobile($email,$password);
        $login_token = trim($rev->token);
        $member_id = $rev->member_id;
        $params = array();
        $params['user']          = $email;
        $params['pass']          = $password;
        $params['login_id']      = OAuthUtils::getLoginIdByEmail($email);
        $params['token']         = $login_token;
        $params['request_token'] = NULL;
        $params['msisdn_id']     = $msisdn_id;
        $params['member_id'] = $member_id;
        if (OAuthModelConfig::USING_MEM_SAVE_MEMBER) {
            OAuthUtils::setUserInfo($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        }
        //save user's info to memcache & save cookie info.
        $rec = OAuthUtils::saveUserInfoToMc($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
        $rec = OAuthUtils::saveLoginInfo($email,$lang);
        
        #step II ,save user info to mc.
        $user_account_email = $email;   //(isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
        $user_page_lang     = (isset($_REQUEST['p_lang'])) ? $_REQUEST['p_lang'] : ((!empty($lang)) ? $lang : OAuthConfig::DEFAULT_SUPPORT_LANG);
        $pkg_id             = (isset($_REQUEST['pkg_id'])) ? $_REQUEST['pkg_id'] : NULL;
        $pkg_version        = (isset($_REQUEST['pkg_v'])) ? $_REQUEST['pkg_v'] : NULL;
        $pukey              = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;

        //check vars
        if (empty($pkg_id) || empty($pkg_version)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1013,$lang,'','package_info');
            exit; 
        }
        $ptoken = OAuthUtils::getPToken();
        if (empty($pukey) || empty($ptoken)) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','PUKey');
            exit;
        }
        
        //bind datas.
        $fdata = array();
        $tmpData = OAuthUtils::getFormData($pukey,$ptoken);
        if (!$tmpData->retFlag) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','getFormData');
            exit;
        }

        $tmpData = $tmpData->data;
        $fdata['m_login_id']      = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
        $fdata['m_call_back']     = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
        $fdata['m_lang']          = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
        $fdata['m_reback_url']    = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
        $fdata['m_model']         = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
        $fdata['p_plan_id']       = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
        $fdata['p_version_id']    = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
        $fdata['current_page']    = (isset($tmpData->current_page)) ? $tmpData->current_page : OAuthConfig::PAGE_PLAN_INFORMATION;
        $fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
        $fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
        $fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
        $fdata['m_step_back_IV']  = (isset($tmpData->m_step_back_IV)) ? $tmpData->m_step_back_IV : '';
        $fdata['m_step_back_V']   = (isset($tmpData->m_step_back_V)) ? $tmpData->m_step_back_V : '';
        $fdata['p_bill_address']  = $user_account_email;
        $fdata['p_lang']          = $user_page_lang;
        $ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
        if (!$ret) {
            echo OAuthModelConfig::getAjaxErrInfo(0,1023,$lang,'','setFormData');
            exit;
        }
        $result['retFlag'] = 1;
        echo json_encode($result);
        break;/*}}}*/
    case 'logout':/*{{{*/
            //clear cookie and logout from mobile.
            $call_back   = (isset($_REQUEST['call_back']) && !empty($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL;
            $reback_url  = (isset($_REQUEST['reback_url']) && !empty($_REQUEST['reback_url'])) ? $_REQUEST['reback_url'] : NULL;
            $lang        = (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) ? $_REQUEST['lang'] : OAuthConfig::DEFAULT_SUPPORT_LANG;
            $oauth_token = (isset($_REQUEST['oauth_token']) && !empty($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL;
            $params      = array();
            //array setting.
            $params['call_back']  = $call_back;
            $params['reback_url'] = $reback_url;
            $params['lang']       = $lang;
            //url setting.
            $redirect = OAuthConfig::OAuthGetBuyPlanFirst($params);
            $login_id = OAuthUtils::getDefaultLoginId();
            $result   = array('retFlag'=>TRUE,'data'=>$redirect);
            if (empty($login_id)) {
                echo json_encode($result);
                exit;
            }

            $userInfo = OAuthUtils::getUserInfoFromMc($login_id);
            $token    = (isset($userInfo['token'])) ? $userInfo['token'] : NULL;
            //empty token or empty userInfo will redirect . 
            if (empty($userInfo) || empty($token)) {
                echo json_encode($result);
                exit;
            }
            
            //can get login id and token will logout for MS-back-end.
            if (!empty($login_id) && !empty($token)) {
                $ret = OAuthUtils::logoutFormMobile($login_id,$token);
            }
            echo json_encode($result);
            exit;
        break;/*}}}*/
    default:
        echo OAuthModelConfig::getAjaxErrInfo(0,1000,$lang,'','other');
        break;
}

?>
